//
//  SHEmoticonPageViewCell.swift
//  SHWeibo
//
//  Created by jingming wang on 16/8/9.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

class SHEmoticonPageViewCell: UICollectionViewCell {
    
    // 保存20个按钮数组
    var buttons:[SHEmoticonButton] = [SHEmoticonButton]()
    
    // 模型数组
    var emoticons: [SHEmoticon]? {
        didSet {
            // 隐藏所有按钮
            for value in buttons {
                value.hidden = true
            }
            // 遍历
            for (i, emoticon) in emoticons!.enumerate() {
                // 获取对应的button
                let button = buttons[i]
                // 赋值
                button.emoticon = emoticon
                // 显示按钮
                button.hidden = false
                // 如果是emoji
                if emoticon.type! == "1"  {
                    button.setTitle((emoticon.code ?? "" as NSString).emoji(), forState: .Normal)
                    // 解决image和title 重复问题
                    button.setImage(nil, forState: .Normal)
                }else {
                    // 代表图片表情
                    let image = UIImage.getEmoticonImage(emoticon.path)
                    button.setImage(image, forState: .Normal)
                    // 解决image和title 重复问题
                    button.setTitle(nil, forState: .Normal)
                }
            }
        }
    }
    
    // MARK: - 懒加载控件
    // 删除按钮
    private lazy var deleteButton: UIButton = UIButton(imgName: "compose_emotion_delete", isHighlighted: true, target: self, action: "didClickDelete")
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        addChildButtons()
        contentView.addSubview(deleteButton)
    }
    
    // MARK: - 添加按钮群
    private func addChildButtons() {
        for _ in 0..<SHEmoticonMaxCount {
            let button = SHEmoticonButton(target: self, action: "didClickEmoticon:")
            button.titleLabel?.font = Font(30)
            contentView.addSubview(button)
            buttons.append(button)
        }
    }
    // 重新布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 按钮的宽高
        let margin: CGFloat = 5
        let childW = (self.frame.width - margin*2) / CGFloat(SHEmoticonMaxCol)
        let childH = (self.frame.height - margin*4) / CGFloat(SHEmoticonMaxRow)
        // 遍历按钮数组 给button设置frame
        for (i,value) in buttons.enumerate() {
            let col = CGFloat(i%SHEmoticonMaxCol)
            let row = CGFloat(i/SHEmoticonMaxCol)
            value.frame = CGRect(x: childW*col + margin, y: childH*row, width: childW, height: childH)
        }
        // 设置删除按钮的frame
        deleteButton.frame = CGRect(x: self.frame.width - childW - margin, y: childH*2, width: childW, height: childH)
    }
    
    // MARK: - 删除按钮监听
    @objc private func didClickDelete() {
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonDeleteButtonNoti, object: nil)
    }
    // MARK: - 表情按钮监听
    @objc private func didClickEmoticon(sender: SHEmoticonButton) {
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonButtonsNoti, object: sender.emoticon)
    }

}

